package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.FinancialWarningDAO;
import cn.edu.nju.software.greencreditsystem.domain.FinancialWarning;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-8
 * 类描述：财务警告DAO实现
 */
public class FinancialWarningDAOImpl implements FinancialWarningDAO
{

    @Override
    public boolean save(FinancialWarning fw)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(fw);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(FinancialWarning fw)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(fw);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(FinancialWarning fw)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(fw);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialWarning getById(int id)
    {
        FinancialWarning result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (FinancialWarning) s.get(FinancialWarning.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialWarning> getByEnterprise(int enterpriseID)
    {
        List<FinancialWarning> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialWarning where enterprise.id=" + enterpriseID + " order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialWarning> getEnterpriseUnread(int enterpriseID)
    {
        List<FinancialWarning> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialWarning where enterprise.id=" + enterpriseID
                                    + " and enterpriseReaded = false order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
    @Override
    public List<FinancialWarning> getAll()
    {
        List<FinancialWarning> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialWarning order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialWarning> getBankUnread(int enterpriseID)
    {
        List<FinancialWarning> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialWarning where enterprise.id = " + enterpriseID
                                    + " and bankReaded = false order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialWarning> getBankUnread()
    {
        List<FinancialWarning> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialWarning where bankReaded = false order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
